package com.task.controller;

import com.github.pagehelper.PageInfo;
import com.task.common.ReturnResult;
import com.task.pojo.Task;
import com.task.server.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 可选
 *
 * @author JiaFuJun
 * @version v1.0
 * @date 2019-12-13
 */
@Api("工作管理")
@RestController
@RequestMapping(value = "/task")
public class TaskController {
    @Autowired
    private TaskService taskservice;

    @ApiOperation("保存数据")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ReturnResult save(HttpServletRequest request, HttpServletResponse response, Task record) {
        if (StringUtils.isEmpty(record.getTaskName())) {
            return ReturnResult.build("任务名称不能为空");
        }
        record.setTaskDescribe(record.getTaskDescribe().replace("\n", "<br/>"));
        taskservice.save(record);
        return ReturnResult.ok();
    }

    @ApiOperation("删除数据")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ReturnResult del(HttpServletRequest request, HttpServletResponse response, Integer id) {
        if (id == null) {
            return ReturnResult.build("id不能为空");
        }

        taskservice.del(id);
        return ReturnResult.ok();
    }

    @ApiOperation("根据主键查询")
    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    public ReturnResult getById(HttpServletRequest request, HttpServletResponse response, Integer id) {
        if (id == null) {
            return ReturnResult.build("id不能为空");
        }

        Task info = taskservice.selectByPrimaryKey(id);
        return ReturnResult.ok(info);
    }

    @ApiOperation("查询列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ReturnResult getList(HttpServletRequest request, HttpServletResponse response, Task record) {
        /**条件判断**/

        List<Task> list = taskservice.getList(record);
        return ReturnResult.ok(list);
    }

    @ApiOperation("查询分页")
    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public ReturnResult pageList(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize, Task record) {
        /**条件判断**/
        if (!StringUtils.isEmpty(record.getTaskName())) {
            record.setTaskName("%" + record.getTaskName() + "%");
        }
        PageInfo<Task> info = taskservice.pageList(record, pageNo, pageSize);
        return ReturnResult.ok(info.getList(), (int) info.getTotal());
    }

    @ApiOperation("修改工作状态")
    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    public ReturnResult updateState(Task record) {
        if (record.getId() == null) {
            return ReturnResult.build("id不能为空");
        }
        if (record.getState() == null) {
            return ReturnResult.build("状态不能为空");
        }
        Task task = taskservice.selectByPrimaryKey(record.getId());

        if (record.getState().equals(task.getState())) {
            return ReturnResult.build("相同状态不能切换");
        }

        if (record.getState().equals(3) && !task.getState().equals(1)) {
            return ReturnResult.build("暂停只能在进行状态使用");
        }

        if (task.getState().equals(3) && !record.getState().equals(1)) {
            return ReturnResult.build("项目暂停中，请先结束暂停");
        }

        taskservice.update(record);
        return ReturnResult.ok();
    }
}